#    MythMobile, a Django interface to MythTV for mobile devices
#    Copyright (C) 2011 Andrew Wilkinson (andrewjwilkinson@gmail.com)
#
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License along
#     with this program; if not, write to the Free Software Foundation, Inc.,
#     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import db_connection
from live_tv_processor import LiveTVProcessor

live_tv_processors = {}

def begin_live_tv(format, channel_no):
    if channel_no not in live_tv_processors:
        live_tv_processors[channel_no] = LiveTVProcessor(args=(format, channel_no))
        live_tv_processors[channel_no].start()

    return "ok"

def get_playlist_entries(format, channel_no):
    if channel_no not in live_tv_processors:
        return []
    else:
        live_tv_processors[channel_no].reset_timer()

        return live_tv_processors[channel_no].segmenter.files

def get_channel_list():
    return db_connection.channel_list()

def get_channel_name(channel_no):
    return db_connection.channel_name(channel_no)

msg_handlers = {
        "begin_live_tv": begin_live_tv,
        "get_channel_list": get_channel_list,
        "get_channel_name": get_channel_name,
        "get_playlist_entries": get_playlist_entries
    }
